package com.example.photo_sharing;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.photo_sharing.page.others.Edit;
import com.example.photo_sharing.tool.Impl.UserInfoSPImpl;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {


    private SharedPreferences sharedPreferences;
    private UserInfoSPImpl usp ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sharedPreferences = getSharedPreferences("userInfo",MODE_PRIVATE);
        usp = new UserInfoSPImpl(sharedPreferences);

        //隐藏标题栏
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide(); //隐藏标题栏
        }

        // 获取页面上的底部导航栏控件
        BottomNavigationView navView = findViewById(R.id.navigation_view);

        // 配置navigation与底部菜单之间的联系
        // 底部菜单的样式里面的item里面的ID与navigation布局里面指定的ID必须相同，否则会出现绑定失败的情况

        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_index,R.id.navigation_find,R.id.navigation_message,R.id.navigation_mine).build();
        // 建立fragment容器的控制器，这个容器就是页面的上的fragment容器
        NavController navController = Navigation.findNavController(this, R.id.navigation_fragment);

        // 启动
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        //每次运行程序初始化userInfo(sharepreferences)，如果用户为登陆状态数据不变，用户非登陆状态数据清除
        boolean isLogin = usp.isLogin();
        if(!isLogin){
            usp.exitLogin();
        }

        //添加发布图片的点击事件
        ImageView imageView = (ImageView)findViewById(R.id.navigation_edit_btn);
        imageView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(isLogin){
                    Intent i = new Intent(MainActivity.this , Edit.class);
                    i.putExtra("signOfOld",false);
                    startActivity(i);
                }
                else{
                    Toast.makeText(MainActivity.this,"请先登录",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}